$(function () {
    renderUser()
  })
  
  async function getProviderById() {
    let { id } = getParams() //{id:'40'} 对象解构
    if (!id) {
      return null
    }
    //准备参数
    let options = {
      url: 'http://146.56.230.35:3007/provider/getProviderById',
      type: 'GET',
      data: { id: parseInt(id) },
    }
    try {
      let res = await request(options)
      console.log(res)
      if (res.code !== 200) {
        alert(res.msg)
        return null
      }
      return res.provider
    } catch (error) {
      return null
    }
  }
  
  async function renderUser() {
    let user = await getProviderById()
    if (!user) {
      return
    }
    let userView = $('#userView')
    userView.empty()
    let str = `
      <p><strong>供应商编码：</strong><span>${user.proCode}</span></p>
      <p><strong>供应商名称：</strong><span>${user.proName}</span></p>
      <p><strong>联系人：</strong><span>${user.proContact}</span></p>
      <p><strong>联系电话：</strong><span>${user.proPhone}</span></p>
      <p><strong>传真：</strong><span>${user.proFax}</span></p>
      <p><strong>描述：</strong><span>${user.proDesc}</span></p>
      <a href="providerList.html">返回</a>
    `
    userView.append(str)
  }
  
  //获得url中的查询参数
  function getParams() {
    //?id=40&name=tom
    let search = location.search
    if (!search) {
      return null
    }
    search = search.substring(1)
    let index = search.indexOf('&')
    let object = {}
    if (index === -1) {
      let arr = search.split('=')
      object[arr[0]] = arr[1]
    } else {
      //id=40&name=tom--->arr=['id=40','name=tom']
      let arr = search.split('&')
      for (let i = 0; i < arr.length; i++) {
        //'id=40'
        let item = arr[i]
        //'id=40'--->['id','40']
        let array = item.split('=')
        object[array[0]] = array[1]
      }
    }
    return object
  }